package com.xdtech.hotel.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.thoughtworks.xstream.XStream;
import com.xdtech.common.utils.JsonUtil;
import com.xdtech.hotel.model.DeviceLink;
import com.xdtech.hotel.model.DeviceLinkOption;
import com.xdtech.hotel.model.RoomConfig;
import com.xdtech.hotel.protocol.vo.Ac;
import com.xdtech.hotel.protocol.vo.Component;
import com.xdtech.hotel.protocol.vo.Dimmer;
import com.xdtech.hotel.protocol.vo.Music;
import com.xdtech.hotel.protocol.vo.Node;
import com.xdtech.hotel.protocol.vo.Tv;
import com.xdtech.hotel.service.DeviceLinkService;
import com.xdtech.hotel.service.RoomConfigService;
import com.xdtech.hotel.util.HotelConstants;
import com.xdtech.hotel.vo.DeviceLinkItem;
import com.xdtech.hotel.vo.RoomConfigItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-04-04 11:42:14
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class RoomConfigController {
	@Autowired
	private RoomConfigService roomConfigService;
	@Autowired
	private DeviceLinkService deviceLinkService;
	@RequestMapping(value="/roomConfig.do",params = "roomConfig")
	public ModelAndView skipRoomConfig() {
		return new ModelAndView("hotel/roomConfig/roomConfig_ftl");
	}
	
	@RequestMapping(value="/roomConfig.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处理
			results = roomConfigService.loadPageAndCondition(null, null);
		}else {
			results = roomConfigService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(value="/roomConfig.do",params = "editRoomConfig")
	public ModelAndView editRoomConfig(HttpServletRequest request,Long roomConfigId) {
		if (roomConfigId!=null) {
			request.setAttribute("roomConfigItem", roomConfigService.loadRoomConfigItem(roomConfigId));
		}
		return new ModelAndView("hotel/roomConfig/editRoomConfigWithLink_ftl");
	}
	/**
	 * 新增添加客房配置时定义设备元件界面
	 * 
	 * @author max.zheng
	 * @create 2015-6-13下午10:35:54
	 * @modified by
	 * @param request
	 * @param roomConfigId
	 * @return
	 */
//	@RequestMapping(value="/roomConfig.do",params = "editRoomConfigWithLink")
//	public ModelAndView editRoomConfigWithLink(HttpServletRequest request,Long roomConfigId) {
//		if (roomConfigId!=null) {
//			request.setAttribute("roomConfigItem", roomConfigService.loadRoomConfigItem(roomConfigId));
//		}
//		return new ModelAndView("hotel/roomConfig/editRoomConfigWithLink_ftl");
//	}
	
	@RequestMapping(value="/roomConfig.do",params = "saveRoomConfig")
	@ResponseBody
	public ResultMessage saveRoomConfig(RoomConfigItem item) {
		ResultMessage r = new ResultMessage();
		if (roomConfigService.saveOrUpdateRoomConfig(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/roomConfig.do",params = "deleteRoomConfigItems")
	@ResponseBody
	public ResultMessage deleteRoomConfigItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> roomConfigIds = new ArrayList<Long>();
			for (String id : tempIds) {
				roomConfigIds.add(Long.valueOf(id));
			}
			roomConfigService.deleteRoomConfigInfo(roomConfigIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	@RequestMapping(value="/roomConfig.do",params = "saveRoomConfigInfos")
	@ResponseBody
	public ResultMessage saveRoomConfigInfos(String data) {
		ResultMessage r = new ResultMessage();
//		System.out.println(data);
		Map<String, Class> classMap = new HashMap<String, Class>();
		classMap.put("id", Long.class);
		classMap.put("configInfo", String.class);
		classMap.put("configXml", String.class);
		classMap.put("deviceLinks", DeviceLinkItem.class);
		RoomConfigItem roomConfigItem = (RoomConfigItem) JsonUtil.getObjByJson(data, RoomConfigItem.class,classMap);
		roomConfigService.saveRoomConfigInfos(roomConfigItem);
		return r;
	}
	
	@RequestMapping(value="/roomConfig.do",params = "newSaveRoomConfigInfos")
	@ResponseBody
	public ResultMessage newSaveRoomConfigInfos(String data) {
		ResultMessage r = new ResultMessage();
		try
		{
			System.out.println(data);
			JSONObject jsonObject = new JSONObject(data);
			RoomConfigItem roomConfigItem = new RoomConfigItem();
			String id = jsonObject.getString("id");
			if (StringUtils.isBlank(id))
			{
				roomConfigItem.setId(null);
			}else
			{
				roomConfigItem.setId(Long.valueOf(id));
			}
			
//			roomConfigItem.setConfigInfo(jsonObject.getString("configInfo"));
			roomConfigItem.setRoomType(jsonObject.getString("roomType"));
			JSONArray deviceLinksArray = jsonObject.getJSONArray("deviceLinks");

			for (int i = 0; i < deviceLinksArray.length(); i++) {
				JSONObject deviceLinkJson = deviceLinksArray.getJSONObject(i);
				if (StringUtils.isNotBlank(deviceLinkJson.getString("id"))) {
					roomConfigItem.getDeviceLinks().add(deviceLinkService.getDeviceLinkItem(deviceLinkJson.getLong("id")));
				}
			}
			roomConfigService.saveRoomConfigInfoWithLinks(roomConfigItem);
		}
		catch (JSONException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return r;
	}
	
	@RequestMapping(value="/roomConfig.do",params="loadRoomConfigUnSelectDeviceLinks")
	@ResponseBody
	public Map<String, Object> loadRoomConfigUnSelectDeviceLinks(String roomType) {
		Map<String, Object> results = roomConfigService.loadRoomConfigUnSelectDeviceLinks(roomType);
		return results;
	}
	@RequestMapping(value="/roomConfig.do",params="loadRoomConfigSelectDeviceLinks")
	@ResponseBody
	public Map<String, Object> loadRoomConfigSelectDeviceLinks(String roomType) {
		Map<String, Object> results = roomConfigService.loadRoomConfigSelectDeviceLinks(roomType);
		return results;
	}
	
	/**
	 * 验证配置是否重复
	 * 
	 * @author max.zheng
	 * @create 2015-4-5下午10:12:54
	 * @modified by
	 * @param id
	 * @param roomType
	 * @return
	 */
	@RequestMapping(value="/roomConfig.do",params = "checkConfigIsExit")
	@ResponseBody
	public boolean checkConfigIsExit(String id,String roomType) {
		Long roomConfigId = StringUtils.isBlank(id)?0L:Long.valueOf(id);
		RoomConfig roomConfig = roomConfigService.getRoomConfigByRoomType(roomConfigId,roomType);
		return roomConfig==null?true:roomConfig.getId().toString().equals(id);
	}

}
